package lws.com.liwushuo.adapter.shouye;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

import lws.com.liwushuo.R;
import lws.com.liwushuo.bean.shouye.SearchSecondBottomBean;
import lws.com.liwushuo.bean.shouye.SearchSecondTopBean;

/**
 * Created by ZQ on 2016/10/18.
 */

public class SearchSecondAdapter extends RecyclerView.Adapter<SearchSecondAdapter.MyHolder> {

    private Context context;
    private SearchSecondTopBean.DataBean topData;
    private SearchSecondBottomBean.DataBean bottomData;
    private List<SearchSecondTopBean.DataBean.PostsBean> topList;//顶部数据 相关攻略
    private List<SearchSecondBottomBean.DataBean.ItemsBean> bottomList;//底部的数据 相关单品
    private int[] layout = {R.layout.shouye_search_second_top_item,
            R.layout.shouye_search_second_bottom_item};

    public SearchSecondAdapter(Context context,SearchSecondTopBean.DataBean topData,SearchSecondBottomBean.DataBean bottomData) {
        this.context = context;
        this.topData=topData;
        this.bottomData=bottomData;
        this.topList=topData.getPosts();
        this.bottomList=bottomData.getItems();
    }

    /**
     * 添加顶部数据
     *
     */
    public void addTopData(SearchSecondTopBean.DataBean topData) {
        this.topData=topData;
        this.topList.addAll(topData.getPosts());
    }

    /**
     * 添加底部数据
     *
     */
    public void addBottomData(SearchSecondBottomBean.DataBean bottomData) {
        this.bottomData=bottomData;
        this.bottomList.addAll(bottomData.getItems());
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(layout[viewType], parent, false);
        return new MyHolder(view);
    }

    @Override
    public int getItemViewType(int position) {
        if (position < 2) {
            return 0;
        } else {
            return 1;
        }
    }

    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
        if (position<2){//topItem布局
            if (position==0){//只有第一个有头部
                holder.itemStickHeader.setVisibility(View.VISIBLE);
                holder.itemStickTitle.setText("相关攻略("+topData.getTotal()+")");
            }
            holder.topItemImg.setImageURI(topList.get(position).getCover_image_url());
            holder.topItemImg.setAspectRatio(2f);
            holder.topItemTitle.setText(topList.get(position).getTitle());
            holder.topItemTime.setText(topList.get(position).getPublished_at()+"");
            holder.topItemAuthor.setText(topList.get(position).getAuthor().getNickname());
        }else{
            if (position==2){//bottom的第一个顶部需要显示
                holder.itemStickHeader.setVisibility(View.VISIBLE);
                holder.itemStickTitle.setText("相关单品("+bottomData.getTotal()+")");
            }else{
                holder.itemStickHeader.setVisibility(View.GONE);
            }
            int currentPosition=(position-2)*2;//先减去前面的两个，每次加载两个
            //左边的数据
            SearchSecondBottomBean.DataBean.ItemsBean itemsBean1 = bottomList.get(position);
            holder.bottomItemImg1.setImageURI(itemsBean1.getCover_image_url());
            holder.bottomItemImg1.setAspectRatio(1f);
            if (itemsBean1.getSelection()==1){
                holder.bottomItemIsJinxuan1.setVisibility(View.VISIBLE);
            }else {
                holder.bottomItemIsJinxuan1.setVisibility(View.GONE);
            }
            holder.bottomItemTitle1.setText(itemsBean1.getShort_description());
            holder.bottomItemSubTitle1.setText(itemsBean1.getName());
            holder.bottomItemPrice1.setText(itemsBean1.getPrice());
            //右边的数据
            SearchSecondBottomBean.DataBean.ItemsBean itemsBean2 = bottomList.get(currentPosition+1);
            holder.bottomItemImg2.setImageURI(itemsBean2.getCover_image_url());
            if (itemsBean2.getSelection()==1){
                holder.bottomItemIsJinxuan2.setVisibility(View.VISIBLE);
            }else {
                holder.bottomItemIsJinxuan2.setVisibility(View.GONE);
            }
            holder.bottomItemTitle2.setText(itemsBean2.getShort_description());
            holder.bottomItemSubTitle2.setText(itemsBean2.getName());
            holder.bottomItemPrice2.setText(itemsBean2.getPrice());
        }
    }

    @Override
    public int getItemCount() {
        int cnt = topList.size() + bottomList.size() / 2;
        return cnt;

    }

    public static class MyHolder extends RecyclerView.ViewHolder {

        //item的stick头部
        private View itemStickHeader;
        private TextView itemStickTitle;
        private TextView itemStickRightBtn;
        //topite布局
        private SimpleDraweeView topItemImg;
        private TextView topItemTitle;
        private TextView topItemTime;
        private TextView topItemAuthor;
        //bottom布局
        //左边布局
        private SimpleDraweeView bottomItemImg1;
        private ImageView bottomItemIsJinxuan1;
        private TextView bottomItemTitle1;
        private TextView bottomItemSubTitle1;
        private TextView bottomItemPrice1;
        //右边布局
        private SimpleDraweeView bottomItemImg2;
        private ImageView bottomItemIsJinxuan2;
        private TextView bottomItemTitle2;
        private TextView bottomItemSubTitle2;
        private TextView bottomItemPrice2;

        public MyHolder(View itemView) {
            super(itemView);

            //粘性头部
            itemStickHeader = itemView.findViewById(R.id.shouye_search_second_itemstickHeader);
            itemStickTitle = ((TextView) itemView.findViewById(R.id.shouye_search_second_itemstickHeader_title));
            itemStickRightBtn = ((TextView) itemView.findViewById(R.id.shouye_search_second_itemstickHeader_rightBtn));
            //topItem布局
            topItemImg = ((SimpleDraweeView) itemView.findViewById(R.id.shouye_search_second_topItem_Img));
            topItemTitle = ((TextView) itemView.findViewById(R.id.shouye_search_second_topItem_title));
            topItemTime = ((TextView) itemView.findViewById(R.id.shouye_search_second_topItem_time));
            topItemAuthor = ((TextView) itemView.findViewById(R.id.shouye_search_second_topItem_author));
            //bottomItem布局,
            //左边布局
            bottomItemImg1 = ((SimpleDraweeView) itemView.findViewById(R.id.shouye_search_second_bottomItem_Img1));
            bottomItemIsJinxuan1 = ((ImageView) itemView.findViewById(R.id.shouye_search_second_bottomItem_isJinxuan1));
            bottomItemTitle1 = ((TextView) itemView.findViewById(R.id.shouye_search_second_bottomItem_title1));
            bottomItemSubTitle1=((TextView) itemView.findViewById(R.id.shouye_search_second_bottomItem_subTitle1));
            bottomItemPrice1=((TextView) itemView.findViewById(R.id.shouye_search_second_bottomItem_price1));
            //右边布局
            bottomItemImg2= ((SimpleDraweeView) itemView.findViewById(R.id.shouye_search_second_bottomItem_Img2));
            bottomItemIsJinxuan2 = ((ImageView) itemView.findViewById(R.id.shouye_search_second_bottomItem_isJinxuan2));
            bottomItemTitle2 = ((TextView) itemView.findViewById(R.id.shouye_search_second_bottomItem_title2));
            bottomItemSubTitle2=((TextView) itemView.findViewById(R.id.shouye_search_second_bottomItem_subTitle2));
            bottomItemPrice2=((TextView) itemView.findViewById(R.id.shouye_search_second_bottomItem_price2));

        }
    }


}
